package com.ly.service.impl;

import com.ly.dao.ProductMapper;
import com.ly.pojo.domain.ProductDO;
import com.ly.pojo.dto.ProductDTO;
import com.ly.pojo.query.ProductQuery;
import com.ly.service.ProductsService;
import java.lang.Boolean;
import java.lang.Override;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ly.exception.BusinessException;
import java.util.Map;
import com.ly.constant.ResultCodeConstant;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;

/**
 * 产品管理模块的实现
 *
 * @author Administrator
 * @date 2025-03-07 10:51:13
 */
@Service
public class ProductsServiceImpl implements ProductsService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public Boolean addProduct(ProductDTO productDTO) {
        // 检查 productDTO 是否为 null
        if (productDTO == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, "产品信息不能为空");
        }
        String productName = productDTO.getProductName();
        // 检查 productName 是否为 null
        if (productName == null || StringUtils.isBlank(productName)) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, "产品信息不能为空");
        }
        ProductDO existingProduct = productMapper.selectByName(new ProductDO());
        if (existingProduct != null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        // 将产品数据保存到数据库
        ProductDO productDO = new ProductDO();
        productDO.setName(productDTO.getProductName());
        productDO.setDescription(productDTO.getDescription());
        productDO.setPrice(productDTO.getPrice());
        productDO.setImageUrl(productDTO.getImageUrl());
        // 创建人信息需要根据实际情况设置
        productDO.setCreateBy("user");
        productDO.setCreateTime(new Date());
        // 修改人信息需要根据实际情况设置
        productDO.setUpdateBy("user");
        productDO.setUpdateTime(new Date());
        int result = productMapper.insert(productDO);
        return result > 0;
    }

    @Override
    public Boolean updateProduct(ProductDTO productDTO) {
        // 检查产品信息是否存在
        ProductDO existingProduct = productMapper.selectById(productDTO.getProductId());
        if (existingProduct == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        // 更新产品信息
        ProductDO productDO = new ProductDO();
        productDO.setId(productDTO.getProductId());
        productDO.setName(productDTO.getProductName());
        productDO.setDescription(productDTO.getDescription());
        productDO.setPrice(productDTO.getPrice());
        productDO.setImageUrl(productDTO.getImageUrl());
        // 修改人信息需要根据实际情况设置
        productDO.setUpdateBy("user");
        productDO.setUpdateTime(new Date());
        int result = productMapper.updateById(productDO);
        return result > 0;
    }

    @Override
    public Boolean deleteProduct(ProductQuery productQuery) {
        // 检查产品信息是否存在
        ProductDO existingProduct = productMapper.selectById(productQuery.getProductId());
        if (existingProduct == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        // 删除产品信息
        int result = productMapper.deleteById(productQuery.getProductId());
        return result > 0;
    }

    @Override
    public ProductDO getProductInfo(ProductQuery productQuery) {
        // 查询指定产品信息
        ProductDO productDO = productMapper.selectById(productQuery.getProductId());
        if (productDO == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        return productDO;
    }
}
